<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
/**
 * 首页 控制器
 *
 * @author <judasnow@gmail.com>
 */
class Base extends CI_Controller{

	public function __construct(){
		parent::__construct();
		$this->load->library( 'user_auth' );
	}
	
	//显示首页
	public function index(){
		if( $this->user_auth->is_login() ){
			//已经登录系统
			//得到用户的信息
			$user = $this->session->userdata( 'user' );
			$user_obj = unserialize( $user['user_obj'] );
			$subject_user_info = $user_obj->get();
		 }else{
			$subject_user_info = array();
		 }
		 //获得首页需要显示的商品信息 (show 字段为 1 的)
		 $this->load->model( 'product_class_model' , '' , TRUE );
		 $all_product_class = $this->product_class_model->find_all( TRUE );		 
		 //$this->output->cache( 95599 );
		 $this->load->view( 'base/home' , array( 'subject_user_info'=>$subject_user_info , 'all_product_class'=>$all_product_class ) );
	}

	private function get_product_list(){
	/*{{{*/
		 $product_class_name = $this->input->get( 'product_class_name' , TRUE );
		 $offset = $this->input->get( 'pager' , TRUE );
		 $this->load->model( 'product_class_model' , '' , TRUE );
		 //获取该类所有商品的信息
		 //获得 class_id 
		 $product_class_info = $this->product_class_model->find( array( 'product_class_name'=>$product_class_name ) );
		 $this->load->model( 'product_at_spec_class_model' , '' , TRUE );
		 //获得该服务包含商品的总数
		 $total_rows = $this->product_at_spec_class_model->count_all( 
			 array( 'product_class_id'=>$product_class_info['class_id'] ) );
		 if( empty( $offset ) ){
			 $offset = 0;
		 }
		 $this->load->library('pagination');
		 $this->config->load('pagination');
		 $config = $this->config->item( 'pagination' );
		 $config['base_url'] = '?';	 	 
		 $config['total_rows'] = $total_rows;
		 $config['per_page'] = 5;
		 $this->pagination->initialize( $config );
		 $products = $this->product_at_spec_class_model->find_all(
			 array( 'product_class_id'=>$product_class_info['class_id'] ) , '' , $config['per_page'] , $offset );
		 $pager_links = $this->pagination->create_links();
		 return array( 'products'=>$products , 'total_rows'=>$total_rows , 'pager_links'=>$pager_links );
        }/*}}}*/

	/**
	 * ajax 调用的接口
	 * 返回相应的页面 以及 页面信息
	 *
	 * @return json 
	 */
	public function get_sub_page(){
	/*{{{*/
                //需要查看的子页面 prev 还是 next 
                $command = $this->input->get( 'command' , TRUE );
                $sub_dir_name = $this->input->get( 'sub_dir_name' , TRUE );
                $product_class_name = $this->input->get( 'product_class_name' , TRUE );

                //@todo 判断该服务是否依然有效
                $this->load->model( 'product_class_model' , '' , TRUE );
                $product_class_info = $this->product_class_model->find( array( 'product_class_name'=>$product_class_name ) );
                if( empty( $product_class_info ) ){
                        echo '该服务已被删除';
                        die;
                }

                //标记是否重置当前记录的页面 cursor 信息
                $reset = $this->input->get( 'reset' , TRUE );
                if( $reset == 'true' ){
                        $this->reset_current_page_cursor();
                }

                //learn_more 就是默认 
                $sub_dir_name = $sub_dir_name == 'learn_more' ? 'text_describe' : $sub_dir_name;

                //根据用户输入, 生成当前目录信息
                $current_dir_path = "$product_class_name/$sub_dir_name";

                $current_dir_full_path = $this->session->userdata( 'current_dir_full_path' );
                if( empty( $current_dir_full_path ) ){

                        //生成目录绝对路径
                        $current_dir_full_path = APPPATH . "views/mod/base_index_product_class_infos/$current_dir_path";
                        $this->session->set_userdata( 'current_dir_full_path' , $current_dir_full_path );	 
                }else{

                        //获得 session 中保存的 sub_dir_name
                        $current_dir_full_path_split_with_slash = preg_split( '/\//' , $current_dir_full_path );
                        $sub_dir_name = $current_dir_full_path_split_with_slash[ count( $current_dir_full_path_split_with_slash ) - 1 ];
                }

                //获得目录中 文件列表
                $this->load->helper( 'directory' );
                $current_dir_full_path_map = directory_map( $current_dir_full_path , 1 );
                $file_count = count( $current_dir_full_path_map );
                $file_count--;

                //当前显示的文件 cursor
                $current_file_cursor = $this->session->userdata( 'current_file_cursor' );
                if( !isset( $current_file_cursor ) ){

                        //还米有设置则从 0 开始
                        $this->session->set_userdata( 'current_file_cursor' , 0 );
                        $current_file_cursor = 0;
                }else{

                        //否则根据 commond 确定下一个需要显示的文件
                        switch( $command ){

                        case 'next' :
                                $current_file_cursor += 1;
                                //如果大于 $file_count , 进入下一个文件夹
                                if( $current_file_cursor > $file_count ){

                                        $_GET['command'] = '';
                                        $this->session->set_userdata( 'current_dir_full_path' , '' );
                                        switch( $sub_dir_name ){

                                        case 'text_describe' :
                                                $_GET['sub_dir_name'] = 'multimedia_describe';
                                                break;
                                        case 'multimedia_describe' :
                                                echo $_GET['sub_dir_name'] = 'feature';
                                                break;
                                        case 'feature' :
                                                $_GET['sub_dir_name'] = 'detail_category_list';
                                                break;
                                        case 'detail_category_list' :
                                                $_GET['sub_dir_name'] = 'text_describe';
                                                break;
                                        default :
                                                echo $sub_dir_name;
                                                break;
                                        }

                                        $this->session->set_userdata( 'current_file_cursor' , 0 );
                                        $this->get_sub_page();
die;
                                }
                                break; 
                        case 'priv' :
                                $current_file_cursor -= 1;
                                //如果小于 $file_count , 进入上一个文件夹
                                if( $current_file_cursor < 0 ){

                                        $_GET['command'] = '';
                                        $this->session->set_userdata( 'current_dir_full_path' , '' );
                                        switch( $sub_dir_name ){

                                        case 'text_describe' :
                                                $_GET['sub_dir_name'] = 'detail_category_list';
                                                break;
                                        case 'multimedia_describe' :
                                                $_GET['sub_dir_name'] = 'text_describe';
                                                break;
                                        case 'feature' :
                                                $_GET['sub_dir_name'] = 'multimedia_describe';
                                                break;
                                        case 'detail_category_list' :
                                                $_GET['sub_dir_name'] = 'feature';
                                                break;
                                        default :
                                                echo $sub_dir_name;
                                                break;
                                        }

						$this->session->set_userdata( 'current_file_cursor' , 0 );
						$this->get_sub_page();
						die;
					 }
					 break; 
			 }

			 //保存当前页面信息到 session 
			 $this->session->set_userdata( 'current_file_cursor' , $current_file_cursor );
		 }

		 //如果是 product_list 页面 ，则根据提供的
		 //product_class_name 获取相应的商品列表
		 if( $sub_dir_name == 'detail_category_list' ){
			 
			 $param = $this->get_product_list();
		 }else{
		 
			 $products = array();
			 $param = array( 'products'=>'' , 'total_rows'=>'' , 'pager_links'=>'' );
		 }
		 $current_file_full_path = "$current_dir_full_path/{$current_dir_full_path_map[$current_file_cursor]}";
		 //尝试显示页面
		 $this->dump_sub_page(
			 $product_class_name , 
			 $current_file_full_path ,
			 array( 'products'=>$param['products'] , 'total_rows'=>$param['total_rows'] , 'pager_links'=>$param['pager_links'] ) , 
			 $sub_dir_name
		 );
	}/*}}}*/

        //前台重置页面指针
        public function reset_current_page_cursor(){
                $this->session->unset_userdata( 'current_dir_full_path' );
                $this->session->unset_userdata( 'current_file_cursor' );
        }

        /**
         * 输出相应的子页面
         *
	 * @param string $product_class_name 商品的类别 , 会污染到 view 模板
	 * @param string $current_file_full_path 需要 dump 的文件路径
	 * @param array $param 需要传递给该页面的信息
	 */
	private function dump_sub_page( $product_class_name , $current_file_full_path , $param , $sub_dir_name ){

		if( file_exists( $current_file_full_path ) && !is_dir( $current_file_full_path ) ){
			include( $current_file_full_path );
			printf( '<div style="display:none;"><span class="product_class_name">%s</span><span class="sub_dir_name">%s</span></div>' ,
			       	$product_class_name , $sub_dir_name );

		}else{
		 	echo '404 Not found.';
		 	exit;
		}
	}
}
